import storage from '@system.storage';
import prompt from '@system.prompt';
import router from '@system.router';
import {getPrescritionList} from '../../common/utils/getPrescriptionList';
const CHANGE_PRESCRTION = "prescription";
export default {
    data: {
        //药单数据
        isSendingList:[],//配送中药单数据
        deliveredList:[],//已送达药单数据

        ttt:"123",
        TGA:"YZJ: ",

        state2:"配送中",
        stateFlag2:0,//0表示没有配送中药单，1表示有
        state3:"已送达",
        stateFlag3:0,//0表示没有已送达药单，1表示有

        showInfo:false,
        color1:"#0a59f7",
        color2:"#6f6162",
        title: "",
        tabInfo:[{
                     picPath:"tab1On.png",
                     title:"FSR",
                 },{
                     picPath:"tab2Off.png",
                     title:"新药单",
                 }],
        sendor_not:1,
        user_name:"用户名"
    },
    onInit() {

//        this.Subscribekv();
//        this.UDP();
        this.onShow();

    },
    onShow(){
        var that = this;
        getPrescritionList("配送中",(res)=>{
            this.isSendingList = res;
            console.log("xxx---配送中："+this.isSendingList[0].state)
            //订单分类是否为空
            this.orderClassification();
        })
        getPrescritionList("已送达",(res)=>{
            this.deliveredList = res;
            console.log("xxx---已送达："+this.deliveredList)
            //订单分类是否为空
            this.orderClassification();
        })
        //这里发送tcp同步订单信息
//        if(this.sendor_not==1){
//                    this.Sync();
//            console.info(this.TGA+"Sync")
//        }
        var that = this;
        storage.get({
            key:"user_name",
            success(data){
                if(data!=""){
                    console.info("YZJ---用户名为"+data);
                    that.user_name=data;
                }
            }
        })
    },
    //订阅
    Subscribekv:async function(){
        var that = this;
        try{
            var actionData = {
            };
            actionData=this.prescription;
            var action = {};
            action.bundleName = "com.example.adsharmonyos";
            action.abilityName = "com.example.adsharmonyos.KvStoreAbility";
            action.messageCode = 1005;
            action.data = actionData;
            action.abilityType = 1;
            action.syncOption = 0;
            var result = await FeatureAbility.subscribeAbilityEvent(action,function (res) { //调用订阅服务API
                var r = JSON.parse(res);
                console.log("yzj123")
                console.info(JSON.stringify(res));
                that.prescription=JSON.parse(r.data.newCard);
                console.log("yzj");
                that.sendor_not=0;
                that.onShow();
                that.sendor_not=1;
                console.log("yzj456")
//                console.info(that.TGA+"收到返回结果")
            });
            console.info(this.TGA+"订阅结果" + result);
        }
        catch (pluginError) {
            console.error(this.TGA+"订阅失败:" + JSON.stringify(pluginError));
        }
    },
    //开启UDPsocket
    UDP: async function(){
        var actionData = {};
        var action = {};
        action.bundleName = 'com.example.adsharmonyos';
        action.abilityName = "com.example.adsharmonyos.KvStoreAbility";;
        action.messageCode = 1010;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption =0;
        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        if (ret.code == 0) {
            console.info(this.TGA+'开启UDP的结果:' + JSON.stringify(ret.abilityResult));
        } else {
            console.error(this.TGA+'UDP开启失败:' + JSON.stringify(ret.code));
        }
    },
    Sync: async function(){
        var actionData = {
        };
        actionData=this.prescription;
        var action = {};
        action.bundleName = 'com.example.adsharmonyos';
        action.abilityName = 'com.example.adsharmonyos.KvStoreAbility';
        action.messageCode = 1007;
        action.data = actionData;
        action.abilityType = 1;
        action.syncOption = 0;

        var result = await FeatureAbility.callAbility(action);
        var ret = JSON.parse(result);
        ret = JSON.parse(result);
        console.info(this.TGA+"同步数据的结果: " + result);
        if (ret.code == 0) {
            console.info('plus result is:' + JSON.stringify(ret.abilityResult));
        } else {
            console.error('plus error code:' + JSON.stringify(ret.code));
        }
    },
    change(e){
        console.log(e.index);
        if(e.index==1){
            this.tabInfo[0].picPath="tab1Off.png";
            this.tabInfo[1].picPath="tab2On.png";
        }
        else if(e.index==0){
            this.tabInfo[0].picPath="tab1On.png";
            this.tabInfo[1].picPath="tab2Off.png";
        }
    },
    toController(){
        router.push({
            uri:"pages/controller/controller"
        })
    },
    toMessagePage(){
        router.push({
            uri: 'pages/messagePage/messagePage',
            //            params: {
            //                data1: 'message',
            //                data2: {
            //                    data3: [123, 456, 789]
            //                },
            //            },
        });
    },
    toMenu(){
    this.$element("me").show();
    },
    toLoginpage(){
        router.push({
            uri: "pages/login/login"
        })
    },
    //查看药单并且获取当前药单信息
    toCheckMedList(e){
        console.log("xxx---toCheckMedList");
        let that = this;
        let curIndex=e.target.dataSet.index;
        let state = e.target.dataSet.flag;
        console.log("xxx---flag:"+state+"   index:"+curIndex);
        let curPrescription;
        if(state=="配送中"){
            for (let i = 0; i < that.isSendingList.length; i++) {
                if(that.isSendingList[i].ID==curIndex){
                    curPrescription = that.isSendingList[i];
                }
            }
            router.push({
                uri: 'pages/checkMedList/checkMedList',
                params: {
                    curPrescription:curPrescription
                },
            });
        }
        else if(state=="已送达"){
            for (let i = 0; i < that.deliveredList.length; i++) {
                if(that.deliveredList[i].ID==curIndex){
                    curPrescription = that.deliveredList[i];
                }
            }
            router.push({
                uri: 'pages/checkMedList/checkMedList',
                params: {
                    curPrescription:curPrescription
                },
            });
        }

    },
    toIndex(){
        router.push({
            uri: 'pages/index/index',
            //            params: {
            //                data1: 'message',
            //                data2: {
            //                    data3: [123, 456, 789]
            //                },
            //            },
        });
    },
    toDelivering(){
        this.$element('swiper').swipeTo({index: 0});
        this.color1="#0a59f7";
        this.color2="#6f6162";
    },
    toFinish(){
        this.$element('swiper').swipeTo({index: 1});
        this.color2="#0a59f7";
        this.color1="#6f6162";
    },
    //监听订单分类是否为空
    orderClassification(){
        var that = this;

        console.log("xxx---isSendingList length:"+this.isSendingList.length)
        if(this.isSendingList.length != 0)
            that.stateFlag2=1;
        else
            that.stateFlag2=0;
        console.log("xxx---deliveredList length:"+this.deliveredList.length)
        if(this.deliveredList.length != 0)
            that.stateFlag3=1;
        else
            that.stateFlag3=0;
    },

}
